package com.zz.controller;

import com.zz.common.Log;
import com.zz.common.R;
import com.zz.entity.Club;
import com.zz.service.ClubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 社团信息管理
 * @author zhouzi,zhuyanyan
 */
@RestController
@RequestMapping("/club")
@Slf4j
@CrossOrigin
@Validated
public class ClubController {

    @Autowired
    private ClubService clubService;

    /**
    * （根据社团名称）分页展示社团信息
     * 周紫
     * 测试
    * */
    @GetMapping("/page")
    @Cacheable(cacheNames="clubCache#86400",key = "'club'",sync = true)
    @Log(title = "展示社团信息",dataLocationType = "link")
    @Transactional
    public R<List<Club>> pageClub(@Nullable @RequestParam String clubName, @Nullable @RequestParam String classify){
        return clubService.page(clubName, classify);
    }

    /**
     * 参加社团
     *  测试成功
    * */
    @PutMapping("/join")
    @CacheEvict(value = "clubCache",allEntries = true)
    @Log(title = "普通用户：参加社团",dataLocationType = "link")
    @Transactional
    public R<java.lang.String> joinClub(@NotNull(message = "社团编码不能为空") @RequestParam int clubId,
                                    HttpServletRequest request){

        return clubService.join(clubId, request);
    }

    /**
     * 退出已经参加的社团
     * 测试成功
    */
    @PutMapping("/exit")
    @CacheEvict(value = "clubCache",allEntries = true)//删除clubCache分类下的所有缓存数据
    @Log(title = "普通用户：退出已经参加的社团", dataLocationType = "link")
    @Transactional
    public R<String> exitClub(@NotNull(message = "社团编码不能为空") @RequestParam int clubId,
                          HttpServletRequest request){
        return clubService.exit(clubId, request);
    }

    @GetMapping("/clubInfo")
    @Log(title = "普通用户：查看社团详细信息", dataLocationType = "link")
    public R<Club> clubInfo(@NotNull(message = "社团编码不能为空") @RequestParam int clubId){
        return clubService.clubInfo(clubId);
    }
}
